// forum_register.js
console.log('JavaScript文件已加载');
document.addEventListener('DOMContentLoaded', function() {
    const registerForm = document.getElementById('registerForm');
    const passwordInput = document.getElementById('password');
    const confirmPasswordInput = document.getElementById('confirmPassword');
    const togglePasswordIcons = document.querySelectorAll('.toggle-password');

    console.log('注册页面JavaScript已加载');

    // 密码显示/隐藏功能
    togglePasswordIcons.forEach(icon => {
        icon.addEventListener('click', function() {
            const targetId = this.getAttribute('data-target');
            const targetInput = document.getElementById(targetId);
            const type = targetInput.getAttribute('type') === 'password' ? 'text' : 'password';
            targetInput.setAttribute('type', type);
            this.classList.toggle('fa-eye');
            this.classList.toggle('fa-eye-slash');
        });
    });

    // 实时表单验证
    const inputs = registerForm.querySelectorAll('input[required]');
    inputs.forEach(input => {
        input.addEventListener('blur', function() {
            validateField(this.id);
        });
    });

    confirmPasswordInput.addEventListener('input', function() {
        validateField('confirmPassword');
    });

    // 表单提交处理
    registerForm.addEventListener('submit', function(e) {
        console.log('表单提交事件触发');
        let isValid = true;
        inputs.forEach(input => {
            if (!validateField(input.id)) {
                isValid = false;
            }
        });
        if (isValid) {
            console.log('表单验证通过，即将提交到服务器');
        } else {
            console.log('表单验证失败，阻止提交');
            e.preventDefault();
        }
    });

    // 字段验证函数（与HTML元素ID对应）
    function validateField(fieldId) {
        const input = document.getElementById(fieldId);
        const errorElement = document.getElementById(`${fieldId}Error`);
        let isValid = true;

        if (!errorElement) {
            console.log(`警告: 未找到错误提示元素 ${fieldId}Error`);
            return isValid;
        }

        switch (fieldId) {
            case 'account':
                if (!input.value.trim()) {
                    errorElement.textContent = '账号不能为空';
                    isValid = false;
                } else {
                    errorElement.textContent = '';
                }
                break;
            case 'nickname':
                if (!input.value.trim()) {
                    errorElement.textContent = '昵称不能为空';
                    isValid = false;
                } else {
                    errorElement.textContent = '';
                }
                break;
            case 'password':
                if (!input.value) {
                    errorElement.textContent = '密码不能为空';
                    isValid = false;
                } else if (input.value.length < 6) {
                    errorElement.textContent = '密码至少需要6位';
                    isValid = false;
                } else {
                    errorElement.textContent = '';
                }
                break;
            case 'confirmPassword':
                if (!input.value) {
                    errorElement.textContent = '请确认密码';
                    isValid = false;
                } else if (input.value !== passwordInput.value) {
                    errorElement.textContent = '两次输入的密码不一致';
                    isValid = false;
                } else {
                    errorElement.textContent = '';
                }
                break;
        }

        input.style.border = isValid ? '1px solid #ddd' : '1px solid red';
        return isValid;
    }
});